
function handleHttpResult(response) {
  return new Promise((resolve, reject) => {
    if (response.ok) {
      return resolve(response.text());
    }

    return reject(response);
  });
}

function handleJsonResult(response) {
  return new Promise((resolve, reject) => {
    const json = typeof response === 'string' ? JSON.parse(response) : response;

    if (json.status === '0' || json.status === '100') {
      return resolve(json.body);
    }

    return reject(json);
  });
}

function handleTimeout(response) {
  return new Promise((resolve, reject) => {
    let json = {};
    try {
      json = JSON.parse(response);
    } catch (e) {
      return resolve(response);
    }

    if (json.status !== '99') {
      return resolve(response);
    }

    return reject(json);
  });
}

export {
  handleHttpResult,
  handleJsonResult,
  handleTimeout,
};
